<template>
  <div class="bridge-waiting">
    <component :is="subPage" ref="subPage" render="div" @mounted="pageCreated"></component>
  </div>
</template>
<script>
import { getAction, postAction } from '@tapui/utils/http'
export default {
  data() {
    return {
      loading: true,
      subPage: 'div',
      verifyUrl: '/auth/loginBySecurity',
      d_data: {},
      p_data: {},
      iseditable: 0,
      timer: ''
    }
  },
  methods: {
    initDialog() {
      let that = this
      let a = that.$route.query
      if (that.$route.query.hasOwnProperty('uiOperateState')) {
        that.uiOperateState = that.$route.query.uiOperateState
      }
      that.iseditable = parseInt(that.$route.query.iseditable)
      Object.keys(a).forEach(m => {
        if (m.startsWith('d_')) {
          that.d_data[m.substring(2)] = a[m]
        } else if (m.startsWith('p_')) {
          that.p_data[m.substring(2)] = a[m]
        }
      })
      let component = that.$route.query.component
      const page = () => import('@/views' + component)
      this.subPage = page
      window.parent.postMessage('loadingover', '*')
    },
    getOpenTime() {
      if (this.$refs.subPage && this.$refs.subPage.codeModel) {
        if (Object.keys(this.$refs.subPage.codeModel).length > 0) {
          this.$refs.subPage.open()
          clearInterval(this.timer)
        }
      }
    },
    judgeOpenTime() {
      let that = this
      this.timer = setInterval(function() {
        that.getOpenTime()
      }, 100)
    },
    pageCreated() {
      let that = this
      Object.keys(this.d_data).forEach(m => {
        that.$refs.subPage[m] = that.d_data[m]
      })
      // this.$refs.subPage.id = that.d_data.id
      if (that.iseditable === 0) {
        that.$refs.subPage.isAdd = true
      } else {
        that.$refs.subPage.isAdd = false
      }
      that.$refs.subPage.visible = true
      that.$refs.subPage.disableEditSubmit = false
      that.$refs.subPage.uiOperateState = this.uiOperateState
      that.judgeOpenTime()
      // that.$refs.subPage.open()
    }
  },
  created() {
    if (this.$store.getters.user.code === this.$route.query.username) {
      this.initDialog()
      // this.$router.push({ path: this.$route.query.url || '/' })
      return
    }
    this.$store.dispatch('LogOut').then((r) => {
      this.$store.dispatch('Token', this.$route.query.token).then(res => {
        this.$store.dispatch('GetInfo').then(res => {
          this.initDialog()
        })
      })
    })
  }
}
</script>
<style scoped lang="scss" rel="stylesheet/scss">
.bridge-waiting {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: white;
  overflow: hidden;
  .center {
    position: absolute;
    width: 100%;
    text-align: center;
    top: 50%;
    margin-top: -250px;
  }
}
</style>
